import os
from pathlib import Path
import shutil
import re

file_dir="E:\周丹丽研究生\语言学python\语料分组"
pattern_blank=" "#文件夹的名字（其实是前后不能带）不带空格
list_none=[]
for root,dirs,files in os.walk(file_dir):
	list_none=[]
	if files==list_none:
		continue
	
	p = Path(root)  # 初始化构造Path对象
	#print(root)

	file_object_list = list(p.glob("./*_chuli.txt"))
	list_lir=[]
	list_repeat=[]
	list_repeat_count=[]
	for file in file_object_list:
		file_object = open(file, 'rU', encoding='UTF-8')
		for line in file_object:
			if line[0]=="(":
				
				tp_l=eval(line)
				str_mark=""
				for i in file.name:
					if i=="%":
						break
					str_mark=str_mark+i
				str_temp=str(str_mark)
				tp_f=eval("(\""+str_temp+"\",)")
				list_lir.append(tp_l+tp_f)
		file_object.close()
	for i in range(0,len(list_lir)):
		if list_lir[i][0]=="":
			continue
		str_list=[]
		count=0
		for j in range(i+1,len(list_lir)):
			if list_lir[i][0]==list_lir[j][0] and list_lir[i][0]!="":
				count=count+1
				if count==1:
					str_list.append(list_lir[i])
					str_list.append(list_lir[j])
					list_temp=list(list_lir[j])
					list_temp[0]=""
					list_lir[j]=tuple(list_temp)
				else:
					str_list.append(list_lir[j])
					list_temp=list(list_lir[j])
					list_temp[0]=""
					list_lir[j]=tuple(list_temp)
		#print(str_list)
		if str_list==list_none:
			continue
		else:
			list_repeat.append(str_list)
			list_repeat_count.append(count)
	#print(list_repeat)
	f=open(root+"\\"+"重复词语.txt",'w',encoding='UTF-8')
	for i in range(0,len(list_repeat)):
		str_line=""
		str_line=str_line+list_repeat[i][0][0]+":"+''.join(str(list_repeat[i]))+"重复次数:"+''.join(str(list_repeat_count[i]))
		#print(str_line)
		f.write(str_line+'\n')
	f.close()
	
		